<template>
  <div>
    <div>
      <el-form :inline="true" :model="form">
        <el-button-group>
          <el-button type="primary" icon="edit" @click="editHandler"/>
          <el-input v-model="form.fileName" clearable placeholder="编号或企业名称" style="width:200px;"/>
          <el-form-item>
            <el-button type="primary" @click="_queryPictureByFileName">搜索</el-button>
          </el-form-item>
        </el-button-group>
      </el-form>
    </div>

    <el-table :data="getSysFiles" height="450" border :stripe="true" :highlight-current-row="true"
              @current-change="tableRowChangeClickHandler"
              style="width: 100%">
      <el-table-column type="index"  label="序号" width='60'/>
      <el-table-column prop="userId" label="企业账号" min-width="20%"/>
      <el-table-column prop="companyName" label="企业名称" min-width="20%"/>
      <el-table-column prop="createDate" label="注册时间" min-width="20%"/>
      <el-table-column prop="STATE" label="文件上传状态" min-width="20%"/>
      <el-table-column inline-template :context="_self" label="操作" min-width="10%">
      <span>
        <el-button type="info" size="small" @click="viewQRLinkHandler($index,row)">查看</el-button>
      </span>
      </el-table-column>
    </el-table>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="getSysFileCurrentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="getFilesCount">
      </el-pagination>
    </div>

    <!--<upload-form ref="uploadForm" :mode="formMode" :param="currentRecord" @initdata="init"/>-->


    <el-dialog title="图片管理" v-model="showQRCodeDialog" @close='dialogclose'>
      <el-row>
        <el-col :span="8" style='font-size: 16px'>营业执照</el-col>
        <el-col :span="8"><img :src="businessLicense" alt="无该图片" style='width: 100px; height: 100px;'></el-col>
        <el-col :span="8">
          <a :href="businessLicense" target='_blank'>
          <el-button size="small" @click="showpicturelist('businessLicense')" :disabled='businessLicense===""'>查看
          </el-button>
          </a>
          <a :href="businessLicense" :download="businessLicense">
            <el-button size="small" :disabled='businessLicense===""'>下载</el-button>
          </a>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8" style='font-size: 16px'>组织机构代码证</el-col>
        <el-col :span="8"><img :src="organizationCodeCertificate" alt="无该图片" style='width: 100px; height: 100px;'>
        </el-col>
        <el-col :span="8">
          <a :href="organizationCodeCertificate" target='_blank'>
          <el-button size="small" @click="showpicturelist('organizationCodeCertificate')"
                     :disabled='organizationCodeCertificate===""'>查看
          </el-button>
          </a>
          <a :href="organizationCodeCertificate" :download="organizationCodeCertificate">
            <el-button size="small" :disabled='organizationCodeCertificate===""'>下载</el-button>
          </a>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8" style='font-size: 16px'>法人代表身份证正面</el-col>
        <el-col :span="8"><img :src="identifyCard_positive" alt="无该图片" style='width: 100px; height: 100px;'></el-col>
        <el-col :span="8">
          <a :href="identifyCard_positive" target='_blank'>
          <el-button size="small" @click="showpicturelist('identifyCard_positive')"
                     :disabled='identifyCard_positive===""'>查看
          </el-button>
          </a>
          <a :href="identifyCard_positive" :download="identifyCard_positive">
            <el-button size="small" :disabled='identifyCard_positive===""'>下载</el-button>
          </a>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8" style='font-size: 16px'>法人代表身份证反面</el-col>
        <el-col :span="8"><img :src="identifyCard_back" alt="无该图片" style='width: 100px; height: 100px;'></el-col>
        <el-col :span="8">
          <a :href="identifyCard_back" target='_blank'>
          <el-button size="small" @click="showpicturelist('identifyCard_back')" :disabled='identifyCard_back===""'>查看
          </el-button>
          </a>
          <a :href="identifyCard_back" :download="identifyCard_back">
            <el-button size="small" :disabled='identifyCard_back===""'>下载</el-button>
          </a>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8" style='font-size: 16px'>银行账户证明文件</el-col>
        <el-col :span="8"><img :src="bankAccountCertificate" alt="无该图片" style='width: 100px; height: 100px;'></el-col>
        <el-col :span="8">
          <a :href="bankAccountCertificate" target='_blank'>
          <el-button size="small" @click="showpicturelist('bankAccountCertificate')"
                     :disabled='bankAccountCertificate===""'>查看
          </el-button>
          </a>
          <a :href="bankAccountCertificate" :download="bankAccountCertificate">
            <el-button size="small" :disabled='bankAccountCertificate===""'>下载</el-button>
          </a>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8" style='font-size: 16px'>经办人身份证正面</el-col>
        <el-col :span="8"><img :src="agentIdentifyCardPositive" alt="无该图片" style='width: 100px; height: 100px;'>
        </el-col>
        <el-col :span="8" >
          <a :href="agentIdentifyCardPositive" target='_blank'>
          <el-button size="small" @click="showpicturelist('agentIdentifyCardPositive')"
                     :disabled='agentIdentifyCardPositive===""'>查看
          </el-button>
          </a>
          <a :href="agentIdentifyCardPositive" :download="agentIdentifyCardPositive">
            <el-button size="small" :disabled='agentIdentifyCardPositive===""'>下载</el-button>
          </a>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8" style='font-size: 16px'>经办人身份证反面</el-col>
        <el-col :span="8"><img :src="agentIdentifyCardBack" alt="无该图片" style='width: 100px; height: 100px;'></el-col>
        <el-col :span="8">
          <a :href="agentIdentifyCardBack" target='_blank'>
          <el-button size="small" @click="showpicturelist('agentIdentifyCardBack')"
                     :disabled='agentIdentifyCardBack===""'>查看
          </el-button>
          </a>
          <a :href="agentIdentifyCardBack" :download="agentIdentifyCardBack">
            <el-button size="small" :disabled='agentIdentifyCardBack===""'>下载</el-button>
          </a>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8" style='font-size: 16px'>授权协议书</el-col>
        <el-col :span="8"><img :src="authorization" alt="无该图片" style='width: 100px; height: 100px;'></el-col>
        <el-col :span="8">
          <a :href="authorization" target='_blank'>
          <el-button size="small" @click="showpicturelist('authorization')" :disabled='authorization===""'>查看
          </el-button>
          </a>
          <a :href="authorization" :download="authorization">
            <el-button size="small" :disabled='authorization===""'>下载</el-button>
          </a>
        </el-col>
      </el-row>
    </el-dialog>

    <!--<el-dialog ref="fileBucketDialog" size="large" title="文件空间管理" v-model="showFileBucketDialog">-->
    <!--<file-bucket-index/>-->
    <!--</el-dialog>-->
  </div>
</template>

<script type="text/babel">
  import {mapActions} from 'vuex'

  import photoshow from './bll/pictureStore'
  import store from '../../store'

  (!store.state.photoshow) && store.registerModule('photoshow', photoshow)

  import UploadForm from './uploadForm.vue';
  import VueQrcode from '../common/vueQRcode.vue';


  export default {
    data() {
      return {
        form: {
          fileName: ''
        },
        formMode: null,
        currentRecord: null,
        pageSize: 10,
        //form: {},
        copyData: '',
        qrData: '',
        privFileUrlFlag: false,
        qrcode: {
          size: 120,
          level: 'L'
        },
        showQRCodeDialog: false,
        showFileBucketDialog: false,
        showimageid: '',
        pictureslist: [],
        businessLicense: '',
        organizationCodeCertificate: '',
        identifyCard_positive: '',
        identifyCard_back: '',
        bankAccountCertificate: '',
        agentIdentifyCardPositive: '',
        agentIdentifyCardBack: '',
        authorization: ''
      }
    },
    components: {UploadForm, VueQrcode},

    computed: {
      getSysFiles() {
        return this.$store.state.photoshow.files;
      },
      getFilesCount() {
        return this.$store.state.photoshow.totalCount;
      },
      getSysFileCurrentPage() {
        return this.$store.state.photoshow.currentPage;
      },
      getSyspictureslist() {
        return this.$store.state.photoshow.pictureslist;
      }
    },

    mounted() {
      console.log('file upload page mounted.');
      this.initPage();
    },

    methods: {
      ...mapActions(['searchFileCount', 'searchFile', 'queryPicture', 'queryPictureCount', 'searchPicturelist']),

      initPage() {
        this.queryPicture();
        this.queryPictureCount();

      },

      init() {
        if (this.form.fileName === '') {
          this.queryPicture();
          this.queryPictureCount();
        }
        this.searchFile({fileName: this.form.fileName});
        this.searchFileCount({fileName: this.form.fileName})
      },


      editHandler() {
        if (!this.currentRecord) {
          this.$message.warning('请选择一条记录');
          return;
        }

        this.$refs.uploadForm.enable();
        this.formMode = 'EDIT';
      },


      fileSpaceHandler() {
        this.showFileBucketDialog = true;
      },


      viewQRLinkHandler($index, row) {
        this.showimageid = row.userId
        this.searchPicturelist({id: this.showimageid, callback: this.getimgurl})
        console.log(this.getSyspictureslist)
        this.showQRCodeDialog = true;
      },

      getimgurl() {
        console.log(1)
        console.log(this.getSyspictureslist)
        for (var i = 0; i < this.getSyspictureslist.length; i++) {
          switch (this.getSyspictureslist[i].fileType) {
            case ('businessLicense'):
              this.businessLicense = this.getSyspictureslist[i].fileUrl
              break;
            case ('organizationCodeCertificate'):
              this.organizationCodeCertificate = this.getSyspictureslist[i].fileUrl
              break;
            case ('identifyCard_positive'):
              this.identifyCard_positive = this.getSyspictureslist[i].fileUrl
              break;
            case ('identifyCard_back'):
              this.identifyCard_back = this.getSyspictureslist[i].fileUrl
              break;
            case ('bankAccountCertificate'):
              this.bankAccountCertificate = this.getSyspictureslist[i].fileUrl
              break;
            case ('agentIdentifyCardPositive'):
              this.agentIdentifyCardPositive = this.getSyspictureslist[i].fileUrl
              break;
            case ('agentIdentifyCardBack'):
              this.agentIdentifyCardBack = this.getSyspictureslist[i].fileUrl
              break;
            case ('authorization'):
              this.authorization = this.getSyspictureslist[i].fileUrl
              break;
          }
        }
      },
      tableRowChangeClickHandler(currentRow, oldCurrentRow) {
        console.log(currentRow);
        this.currentRecord = currentRow;
//        this.$refs.uploadForm.disable();
      },

      handleSizeChange(val) {
        console.log(`每页 ${val} 条`)
        this.pageSize = val;
        if (this.form.fileName === '') {
          this.queryPicture({pageIndex: 1, pageSize: val});
        } else {
          this.searchFile({fileName: this.form.fileName, pageIndex: 1, pageSize: val});
        }
      },
      handleCurrentChange(val) {
        console.log(arguments);
        console.log(`current${val}`);
        if (this.form.fileName === '') {
          this.queryPicture({pageIndex: val, pageSize: this.pageSize});
        } else {
          this.searchFile({fileName: this.form.fileName, pageIndex: val, pageSize: this.pageSize});
        }
      },
      formatterHandler(row, column) {
        switch (column.property) {
          case 'state':
            return row.state === 1 ? 'Yes' : 'Discard';
        }
      },
      _queryPictureByFileName() {
        if (this.form.fileName === '') {
          this.queryPicture({pageSize: this.pageSize});
          this.queryPictureCount({pageSize: this.pageSize});
        }
        this.searchFile({fileName: this.form.fileName, pageSize: this.pageSize});
        this.searchFileCount({fileName: this.form.fileName, pageSize: this.pageSize})
        //重置当前页
        this.getSysFileCurrentPage = 1;
      },

      showpicturelist(type) {
        console.log(1)
        for (var i = 0; i < this.getSyspictureslist.length; i++) {
          console.log(this.getSyspictureslist[i].fileType)
          if (this.getSyspictureslist[i].fileType === type) {
            console.log(this.getSyspictureslist[i].fileUrl)
            //window.open(this.getSyspictureslist[i].fileUrl, '_blank')
          }
        }
      },
      dialogclose() {
        this.businessLicense = ''
        this.organizationCodeCertificate = ''
        this.identifyCard_positive = ''
        this.identifyCard_back = ''
        this.bankAccountCertificate = ''
        this.agentIdentifyCardPositive = ''
        this.agentIdentifyCardBack = ''
        this.authorization = ''
      }
    }
  }
</script>

<style lang="scss">
  .download-url {
    padding: 5px;
  }

  .qrcode-wrapper {
    margin: auto;
  }
</style>

